package com.sdgakj.web.ajax;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springside.modules.web.MediaTypes;

import com.sdgakj.utils.MyUtils;
import com.sdgakj.utils.PropUtil;
import com.sdgakj.utils.QnFileUploadUtil;

/**
 * 人员管理的Controller, :
 * 
 * @author liuwei
 */
@Controller
@RequestMapping(value = "/ajax")
public class AjaxController {

	private static final String DOMAIN_NAME_KEY = PropUtil.getInstance().get("qiniu_domain_name");
	
	@RequestMapping(value = "fileUpload", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = MediaTypes.JSON_UTF_8)
	@ResponseBody
	public String fileUpload(@RequestParam(value = "file") MultipartFile file
			/*@RequestParam(value = "type") String type,@RequestParam(value = "id") Long id*/){
		System.out.println("upload start.....");
		String url = "";
		if(file!=null){
			url = QnFileUploadUtil.uploadFile(file, file.getOriginalFilename());
		}
		return url;
	}

	@RequestMapping(value = "picCut", method = RequestMethod.GET)
	@ResponseBody
	public String picCut(String x, String y, String w, String h, String imageFile) {

		if (!MyUtils.imageCut(imageFile, x, y, w, h)) {
			return "error";
		}
		MyUtils.compressPicByWidthAndHeight(imageFile, 360, 200);
		String url = QnFileUploadUtil.uploadFile(imageFile, null);
		return url;
	}
	
	@RequestMapping(value = "newPicCut", method = RequestMethod.GET)
	@ResponseBody
	public String newPicCut(String x, String y, String w, String h, String imageFile) {
		String fileName = imageFile.substring(imageFile.lastIndexOf("/"));
        String url = "http://"+DOMAIN_NAME_KEY+fileName+"?imageMogr2/crop/!";
        url = url+w+"x"+h+"a"+x+"a"+y;
		return url;
	}
	/***
	 * 上传图片显示高压缩
	 * @param file
	 * @param type
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "imageView2", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = MediaTypes.JSON_UTF_8)
	@ResponseBody
	public String imageView(@RequestParam(value = "file") MultipartFile file,
			@RequestParam(value = "type") String type,@RequestParam(value = "id") Long id){
		String url = "";
		if(file!=null){
			url = QnFileUploadUtil.uploadFile(file, file.getOriginalFilename());
		}
		url= url +"?imageMogr2/auto-orient/strip/quality/75";
		return url;
	}
	
	/***
	 * 下载图片
	 * @param imgurl
	 * @return
	 */
	@RequestMapping(value = "/downimg", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = MediaTypes.JSON_UTF_8)
	public void downimg(@RequestParam(value = "url") String imgurl){
		QnFileUploadUtil.download(imgurl);
	}
}
